//  数据仓库
import { defineStore } from "pinia";
import {
  countGroupByPost,
  pageListPost,
  listTreePost,
  pageCustomPost,
  countCustomPost,
} from "@/api/usemap";
import { GroupType, GroupNum } from "@/uilts";
import type { WarehouseState } from "./types/type";
import columns from "@/views/home/components/column";
import useSettingStore from "@/store/modules/setting";
const SettingStore = useSettingStore();
const useWarehouseStore = defineStore("WarehouseStore", {
  state: (): WarehouseState => {
    return {
      //  地图框选数据类型
      boxSelectionData: [],
      //  地图框选数据列表
      boxSelectionDataList: [],
      //  数据列表
      dataList: [],
      //  总数
      total: 0,
      //  条数
      limit: 10,
      //  页码
      page: 1,
      //  tab类型
      type: "",
      tabType: "变压器",
      //  筛选条件
      searchCondition: {},
      //  筛选列表参数
      selectTotal: 0,
      selectLimit: 10,
      selectPage: 1,
      //  数据列表参数
      params: {},
      //   地图行政区数据
      mapText: {},
      //  查询字段  是否启动
      isEnable: 1,
      //  判断是否是地图高级查询 还是列表高级查询
      disabled: false,
      //  表tree
      treeList: [],
      //  是否高级查询
      isQuery: false,
      //  数据列表但数据详情
      infoList: null,
      //  自定义数据筛选
      searchList: [],
    };
  },
  actions: {
    //  框选点位请求数据类型
    async boxSelectionDataTypeAjax() {
      try {
        const { data } = await countGroupByPost(this.searchCondition);
        if (data) {
          this.mapDataList(data);
        }
      } catch (error) {
        console.log(error);
      }
    },
    //  地图数据列表处理
    mapDataList(data: any) {
      data.forEach((item: any) => {
        item.name = GroupType(item.type);
      });
      this.boxSelectionData = data;
      this.tabType = data[0].name;
      this.type = data[0].type;
      //  显示筛选列表tab及数据
      SettingStore.setColumnName(this.tabType);
      SettingStore.setColumn();
      SettingStore.selectionDataListShow = true;
      this.pageListPostAjax();
    },
    // 框选点位分页查询
    async pageListPostAjax() {
      try {
        if (this.searchList.length > 0) {
          this.searchList.forEach((item: any) => {
            if (item.id === this.type) {
              const obj = item.filters[0];
              this.searchCondition[obj.fieldName] = obj.value;
            }
          });
        }
        if (this.searchCondition.searchParam.length > 0) {
          this.searchCondition.searchParam.forEach((item: any) => {
            if (this.type === GroupNum(item.tableName)) {
              item.filters.forEach((row: any) => {
                row.childList.forEach((val: any) => {
                  this.searchCondition[val.fieldName] = val.value;
                });
              });
            }
          });
          delete this.searchCondition.searchParam;
        }
        const url: any = columns;
        const params = {
          limit: this.selectLimit,
          page: this.selectPage - 1,
          searchCondition: this.searchCondition,
        };
        const { data } = await pageListPost(params, url[this.type]);
        this.boxSelectionDataList = data.content;
        this.selectTotal = Number(data.total);
      } catch (error) {
        console.log(error);
      }
    },
    //  每页条数
    handleSelectSizeChange(value: number) {
      this.selectLimit = value;
      this.pageListPostAjax();
    },
    //  页码
    handleSelectCurrentChange(value: number) {
      this.selectPage = value;
      this.pageListPostAjax();
    },
    //  初始化数据
    clearData() {
      this.boxSelectionDataList = [];
      this.tabType = "";
      this.type = "";
      this.selectLimit = 10;
      this.selectPage = 1;
    },
    //  数据列表tab全部数据
    async dataListTab() {
      try {
        const { data } = await countGroupByPost({});
        if (data) {
          data.forEach((item: any) => {
            SettingStore.columnType.forEach((row: any) => {
              if (item.type === row.type) {
                row.num = item.num;
              }
            });
          });
          this.tabType = SettingStore.columnType[0].label;
          this.type = SettingStore.columnType[0].type;
          SettingStore.setColumnName(SettingStore.columnType[0].label);
          SettingStore.setColumn();
          this.dataListAjax();
        }
      } catch (error) {}
    },
    //  列表数据
    async dataListAjax() {
      try {
        const url: any = columns;
        const params = {
          limit: this.limit,
          page: this.page - 1,
          searchCondition: this.params,
        };
        const ajax = this.isQuery
          ? pageCustomPost(params)
          : pageListPost(params, url[this.type]);
        const { data } = await ajax;
        this.dataList = data.content;
        this.total = Number(data.total);
      } catch (error) {}
    },
    //  每页条数
    handleSizeChange(value: number) {
      this.limit = value;
      this.dataListAjax();
    },
    //  页码
    handleCurrentChange(value: number) {
      this.page = value;
      this.dataListAjax();
    },
    //  tree 地图自定义搜索条件
    async listTreePostAjax() {
      const { data } = await listTreePost();
      this.treeList = data;
    },
    //  自定义数量统计查询
    async countCustomPost(params: any) {
      try {
        const { data } = await countCustomPost(params);
        if (data) {
          this.mapDataList(data);
        }
      } catch (error) {}
    },
  },
  getters: {},
});
export default useWarehouseStore;
